@extends('admin.main')

@section('body')
    <!-- 标题栏 -->
    <div class="main-title">
        <h2>客户订水码列表</h2>
    </div>

    <div class="cf">
        @if(is_administrator())
            <div class="fl">
                <a class="btn" href="javascript:;" onclick="createCode()">生成客户订水码</a>
                <a class="btn" href="javascript:;" onclick="exportCode()">导出excel</a>
                <a class="btn" href="javascript:;" onclick="bindShop()">绑定店铺</a>
            </div>
        @endif

        <div class="search-form fr cf">
            @if(plat_identify() == 'admin')
                <div style="float: left;margin-right: 10px;line-height: 26px;">
                    <select name="type" id="status" style="height: 30px;">
                        <option value="0" @if($codeStatus== 0) selected @endif>全部</option>
                        <option value="1" @if($codeStatus == 1) selected @endif>未绑定店铺</option>
                        <option value="2" @if($codeStatus == 2) selected @endif>未绑定用户</option>
                        <option value="3" @if($codeStatus == 3) selected @endif>已绑定用户</option>
                    </select>
                </div>
            @endif
            <div class="sleft">
                <input type="text" id="keyword" name="keyword" class="search-input" value="{{request('keyword')}}"
                       placeholder="请输入客户订水码">
                <a class="sch-btn" href="javascript:;" id="search" url="{{urlPlat('single','getPrivate')}}"><i class="btn-search"></i></a>
            </div>
        </div>
    </div>
    <div class="data-table table-striped">
        <table class="">
            <thead>
            <tr>
                <th class="">序号</th>
                <th class="">客户订水码</th>
                <th class="">店铺名称</th>
                <th class="">用户名</th>
                <th class="">状态</th>
                @if(plat_identify() == 'account')
                    <th class="">操作</th>
                @endif
            </tr>
            </thead>
            <tbody>
            @if(count($rows['data']))
                @foreach($rows['data'] as $v)
                    <tr>
                        <td>{{$v['id']}}</td>
                        <td>{{$v['code']}}</td>
                        <td>{{$v['shop_name']}}</td>
                        <td>{{$v['nickname']}}</td>
                        <td @if(!$v['status']) style="color: red"@endif>{{$v['status_text']}}</td>
                        @if(plat_identify() == 'account')
                        <td>
                            @if($v['status'] == 1 && $v['code_status'] == 3)
                                <a href="javascript:;" onclick='privateForbid("{{$v['id']}}","{{$v['shop_id']}}")' class="ajax-get" style="color:red">禁用</a>
                            @endif
                        </td>
                        @endif
                    </tr>
                @endforeach
            @else
                <td colspan="12" class="text-center"> aOh! 暂时还没有内容!</td>
            @endif
            </tbody>
        </table>
    </div>
    <div class="page">
        @if($rows['last_page']>1)
            <div>
                <a class="first" href="{{urlPlat('single','getPrivate',['page'=>1,'keyword'=>request('keyword',''),'status'=>request('status',0)])}}">首页</a>
                @if($rows['prev_page_url'])
                    <a class="prev" href="{{$rows['prev_page_url']}}">&lt;&lt;</a>
                @endif
                @if(count($rows['pageNoList']))
                    @foreach($rows['pageNoList'] as $v)
                        @if($rows['current_page'] == $v)
                            <span class="current">{{$v}}</span>
                        @else
                            <a class="num" href="{{urlPlat('single','getPrivate',['page'=>$v,'keyword'=>request('keyword',''),'status'=>request('status',0)])}}">{{$v}}</a>
                        @endif
                    @endforeach
                @endif
                @if($rows['next_page_url'])
                    <a class="next" href="{{$rows['next_page_url']}}">&gt;&gt;</a>
                @endif
                <a class="last" href="{{urlPlat('single','getPrivate',['page'=>$rows['last_page'],'keyword'=>request('keyword',''),'status'=>request('status',0)])}}">末页</a>
                <span class="rows">共 {{$rows['total']}} 条记录</span>
            </div>
        @endif
    </div>
@endsection

@section('script')
    <script src="/static/layer/layer.js"></script>
    <script src="/static/layer/extend/layer.ext.js"></script>
<script type="text/javascript">

    //搜索功能
    $("#search").click(function () {
        var url = $(this).attr('url');
        var query = $('.search-form').find('input').serialize();
        var status = $('#status').val();
        query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g, '');
        query = query.replace(/^&/g, '');
        if (url.indexOf('?') > 0) {
            url += '&' + query + '&status=' + status;
        } else {
            url += '?' + query + '&status=' + status;;
        }
        window.location.href = url;
    });

    function createCode() {
        layer.prompt({
            formType:0,
            title:'请输入生成客户订水码的数量',
        },function(val, index){
            if(val != '' && val != 0) {
                $.ajax({
                    url:"{{urlAdmin('single','postPrivate')}}",
                    data:{num:val},
                    type:'post',
                    success:function(res){
                        if(res.status == 1){
                            layer.msg('生成客户订水码成功');
                            window.location.reload()
                        } else {
                            layer.msg(res.msg)
                        }
                    },
                    fail:function(){
                        layer.msg('生成客户订水码失败')
                    }
                })
            } else {
                layer.msg('请输入正整数')
            }
            layer.close(index);
        });
    }

    function privateForbid(id, shop_id) {
        layer.confirm("禁用后将不可恢复，不可使用，请慎用",{
            title:'禁用客户订水码'
        },function (index) {
            layer.close(index);
            $.ajax({
                type:"POST",
                url:"{{urlPlat('single','anyChangePrivateStatus')}}?status=0&ids="+id+"&shop_id="+shop_id,
                success:function(res) {
                    if(res.status == 1) {
                        layer.msg('禁用成功')
                        window.location.reload()
                    } else {
                        layer.msg('禁用失败')
                    }
                },fail:function () {
                    layer.msg('禁用失败')
                }
            });
        })
    }

    function exportCode() {
        var url = "{{urlAdmin('single','getExportPrivate')}}";
        layer.prompt({
            formType: 0,
            title:'导出excel',
            preTitle:'请输入初始序号及结束序号，中间以"-"隔开，如"1-10"',
            area: ['500px', '200px'],
        }, function(value, index){
            window.location.href = url + '?limit=' + value;
            layer.close(index);

        });

    }

    function bindShop()
    {
        layer.open({
            type: 2,
            title: '绑定店铺',
            area: ['28%','35%'],
            fixed: false,
            content: "{{urlAdmin('single', 'getBindShop')}}"
        });
    }
</script>
@endsection